package com.ruijiang.service.VmService.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruijiang.constant.DefaultNum;
import com.ruijiang.dto.insert.VMInsertDto;
import com.ruijiang.dto.page.VmPageDto;
import com.ruijiang.entity.VmType;
import com.ruijiang.mapper.VmServiceMapper.NodeMapper;
import com.ruijiang.mapper.VmServiceMapper.RegionMapper;
import com.ruijiang.mapper.VmServiceMapper.VmMapper;
import com.ruijiang.mapper.VmServiceMapper.VmTypeMapper;
import com.ruijiang.result.PageResult;
import com.ruijiang.service.VmService.NodeService;
import com.ruijiang.service.VmService.VmService;
import com.ruijiang.vo.node.NodeInfoVO;
import com.ruijiang.vo.region.RegionInfoVO;
import com.ruijiang.vo.vm.VmPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class VmServiceImpl implements VmService {


    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageResult getPageList(VmPageDto vmPageDto) {

        PageHelper.startPage(vmPageDto.getPageIndex(),vmPageDto.getPageSize());
        List<VmPageVO> list = vmMapper.getPageList(vmPageDto);
        //构建元素
        list.stream().forEach(vo -> {
            //查询售货机类型
            vo.setType(vmTypeMapper.getByTypeId(vo.getVmType()));
            //查询点位
            vo.setNode(nodeService.getRegBusByNodeId(vo.getNodeId()));
            //查询区域
            vo.setRegion(regionMapper.getRegionById(vo.getRegionId()));
            //设置默认状态
            vo.setStatus(DefaultNum.StatusMap());
        });
        Page page=(Page) list;
        return PageResult.pageBuild(page);
    }

    /**
     * 添加设备
     * @param dto
     */
    @Override
    public void insertVM(VMInsertDto dto) {
        vmMapper.insertVM(dto);
    }

    /**
     * 修改设备点位
     * @param id
     * @param nodeId
     */
    @Override
    public void updateVMNodeById(String id, String nodeId) {
        vmMapper.updateVMNodeById(id,nodeId);
    }
}
